package com.sh.shanhuo.activity;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sh.shanhuo.R;
import com.sh.shanhuo.adapter.NoticeAdapter;
import com.sh.shanhuo.bean.BaseBean;
import com.sh.shanhuo.bean.NoticeListBean;
import com.sh.shanhuo.listener.GsonObjectCallback;
import com.sh.shanhuo.utils.OkHttpUtil;
import com.sh.shanhuo.utils.ScreenSwitch;
import com.sh.shanhuo.utils.Tools;
import com.sh.shanhuo.view.CommonItemDecoration;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import okhttp3.Call;

public class NoticeActivity extends BaseActivity {
    private NoticeAdapter adapter;
    private String userName;
    private int pageNum;
    private SmartRefreshLayout smartLayout;
    private List<NoticeListBean.RowsBean> rowsBeans = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_notice;
    }

    @Override
    protected void initView() {
        super.initView();
        getBundle();
        ImageView back = findViewById(R.id.back);
        smartLayout = findViewById(R.id.smartLayout);
        smartLayout.setOnLoadMoreListener(refreshLayout -> getNoticeList());
        smartLayout.setOnRefreshListener(refreshLayout -> getNoticeList());
        smartLayout.setEnableLoadMore(true);
        smartLayout.setEnableRefresh(true);
        back.setOnClickListener(v -> finish());
        initNoticeView();
    }

    private void getBundle() {
        Intent intent = getIntent();
        if (intent == null)
            return;
        Bundle bundle = intent.getExtras();
        if (bundle == null)
            return;
        userName = bundle.getString("userName", "");
    }

    private void initNoticeView() {
        RecyclerView noticeView = findViewById(R.id.noticeView);
        noticeView.setLayoutManager(new LinearLayoutManager(this));
        noticeView.addItemDecoration(new CommonItemDecoration(Tools.dp2px(mContext, 10),
                Tools.dp2px(mContext, 10), Tools.dp2px(mContext, 10)));
        adapter = new NoticeAdapter(this, null);
        noticeView.setAdapter(adapter);
        getNoticeList();

        adapter.setOnItemClickListener((adapter, view, position) -> {
            String url = rowsBeans.get(position).getUrl();
            if (!TextUtils.isEmpty(url)) {
                Bundle bundle = new Bundle();
                bundle.putString("url", rowsBeans.get(position).getUrl());
                bundle.putString("id", rowsBeans.get(position).getId() + "");
                ScreenSwitch.startActivity(NoticeActivity.this, WebActivity.class, bundle);
            }
        });
    }

    private void getNoticeList() {
        onShowLoading();
        Map<String, String> params = new HashMap<>();
        params.put("pageSize", "20");
        params.put("pageNum", pageNum + "");
        params.put("orderByColumn", "");
        params.put("isAsc", "");

        Map<String, String> header = new HashMap<>();
        if (!TextUtils.isEmpty(userName)) {
            header.put("userName", userName);
        } else {
            header.put("uuid", Tools.getUniquePsuedoID());
        }
        OkHttpUtil.getInstance().postForm(OkHttpUtil.IP + OkHttpUtil.NOTICE_LIST, header, params, new GsonObjectCallback<NoticeListBean>() {
            @Override
            public void onUi(NoticeListBean noticeListBean) {
                onDismissLoading();
                smartLayout.finishLoadMore();
                smartLayout.finishRefresh();
                if (noticeListBean.getCode() == 200 && noticeListBean.getMsg().equals("查询成功")) {
                    rowsBeans = noticeListBean.getRows();
                    for (NoticeListBean.RowsBean rowsBean : rowsBeans) {
                        readNotice(rowsBean.getId() + "");
                    }
                    Collections.sort(rowsBeans, (o1, o2) -> (int) (Tools.timeSample(o2.getUpdateTime()) -
                            Tools.timeSample(o1.getUpdateTime())));
                    pageNum += noticeListBean.getTotal();
                    adapter.setNewData(rowsBeans);
                }
            }

            @Override
            public void onFailed(Call call, IOException e) {
                smartLayout.finishLoadMore();
                smartLayout.finishRefresh();
                onDismissLoading();
            }
        });
    }

    private void readNotice(String noticeId) {
        Map<String, String> params = new HashMap<>();
        params.put("userName", userName);
        params.put("noticeId", noticeId);
        OkHttpUtil.getInstance().postForm(OkHttpUtil.IP + OkHttpUtil.READ_NOTICE, params, new GsonObjectCallback<BaseBean>() {
            @Override
            public void onUi(BaseBean baseBean) {

            }

            @Override
            public void onFailed(Call call, IOException e) {

            }
        });
    }
}
